<template>
  <div class="content">
    <!-- 渲染表格操作按钮 -->

    <template>
      <el-button
        type="text"
        size="small"
        v-for="itm in btnList"
        :key="itm.id"
        @click.stop="actionHandler(rowObj, itm.name, itm.type)"
        >{{ itm.name }}
      </el-button>
    </template>
  </div>
</template>

<script>
import {
  delDepAxios,
  delRoleAxios,
  alterDepAxios,
  delStaffListAxios,
} from "../http/apis/staffApi/index";
// import { gendanDeleteAxios, gendanUpdateAxios } from "../http/apis/gendanApi";
import { afterSaleDeleteAxios } from "../http/apis/afterSaleApi";
import {
  thoroughDeletedAxios,
  reductionDeletedAxios,
  tochageDeletedAxios,
} from "../http/apis/deletedApi"; //删除
// import { gendanDeleteAxios, gendanUpdateAxios } from "../http/apis/gendanApi";
import {
  DelOrderAxios,
  DelOrderGoodsAxios,
  EditOrderMoneyAxios,
} from "../http/apis/orderApi";
import { mapActions } from "vuex";
import {
  poolListDeleteAxios,
  AuditResultAxios,
  ApplyingAxios,
} from "../http/apis/customerseasApi";
import {
  agreementdel,
  contranctReceivePayment,
} from "../http/apis/agreementApi";
import { delCustomerAxios } from "../http/apis/userManageApi";
import { setStateAxios } from "../http/apis/moneyApi/index";
import {
  getCustomerDataAxios,
  getAddInOutcomeAxios,
} from "../http/apis/moneyApi/index";

export default {
  name: "TableBtn",
  computed: {},
  data() {
    return {
      input1: "",
      input2: "",
    };
  },

  props: {
    btnList: Array,
    rowObj: Object,
  },
  created() {},
  methods: {
    ...mapActions("gendanStore", [
      "gendanDeleteAction",
      "gendanUpdateAction",
      "gendanListAction",
    ]),
    ...mapActions("moneyStore", ["getStateData"]),

    actionHandler(rowObj, btnName, type) {
      if (btnName === "修改") {
        // 跟单修改
        if (type === "跟单") {
          this.$router.push({
            path: "/gendan/gendanedit",
            query: { updateInfo: rowObj },
          });
        }
        //合同修改
        if (type === "合同") {
          this.$router.push({
            path: "/agreement/Edit/?state=0",
            query: rowObj,
          });
        }
        // 订单修改
        if (type === "订单") {
          console.log(rowObj.order_id);
          this.$router.push({
            path: "/order/editorder",
            query: { editInfo: rowObj },
          });
        }
        // 员工管理->员工列表->修改
        if (type === "员工管理") {
          this.$router.push("/staff/alter");
        }
        // 员工管理->角色权限->修改
        if (type === "角色权限") {
          this.$router.push({
            path:"/staff/alter-permissions",
            query: { role: rowObj },
          });

        }
        // 员工管理->部门设置->修改
        if (type === "部门设置") {
          // let id = rowObj.dept_id;
          console.log("修改：", rowObj, btnName, type);
          this.$prompt("修改部门", "修改部门", {
            inputValue: rowObj.dept_name,
            confirmButtonText: "确定",
            cancelButtonText: "取消",
          })
            .then(({ value }) => {
              let arr = {
                dept_id: rowObj.dept_id,
                dept_name: value,
              };
              alterDepAxios(arr)
                .then((res) => {
                  rowObj.dept_name = value;
                  this.$message({
                    type: "success",
                    message: "修改成功:" + value,
                  });
                  console.log("员工管理修改部门成功", res);
                })
                .catch((err) => {
                  this.$message.error("修改错误！");
                  console.log("员工管理修改部门失败", err);
                });
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "取消输入",
              });
            });
        }
        // 财务修改
        if (type === "财务") {
          console.log("财务修改：", rowObj);
          let current;
          if (rowObj.inoutcome == "收入") {
            current = 1;
          } else if (rowObj.inoutcome == "支出") {
            current = 2;
          }
          getAddInOutcomeAxios(current)
            .then((res) => {
              console.log("查询成功：", res.data.data);
              this.$router.push({
                path: "/money/modifyinoutcome",
                query: { detailmsg: rowObj, querymsg: res.data.data },
              });
            })
            .catch((err) => {
              console.log("查询失败：", err);
            });
        }
        //公海修改
        if (type === "公海") {
          console.log(rowObj.customer_name);
          this.$router.push({
            path: "/customerseas/EditList",
            query: { poollistMsg: rowObj },
          });
        }
        //客户修改
        if (type === "客户") {
          this.$router.push({
            path: "/home/adduser",
            query: {
              id: 2,
              ...rowObj,
            },
          });
        }
        if (type === "售后") {
          console.log(rowObj);
          this.$router.push({
            path: "/afterSale/addAfterSale?state=alter&",
            query: { rowObj },
          });
        }

        // 删除
      } else if (btnName === "删除") {
        // 跟单删除
        if (type === "跟单") {
          this.$prompt("请输入删除原因", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(({ value }) => {
              // 删除测试
              this.gendanDeleteAction({
                deletereason: value,
                id: rowObj.documentId.toString(),
              })
                .then(() => {
                  this.$message({
                    type: "success",
                    message: "删除成功",
                  });
                  // 重新请求数据
                  this.$emit("gendanGetData");
                })
                .catch((err) => {
                  console.log("请求失败：", err);
                  this.$message({
                    type: "error",
                    message: "删除失败",
                  });
                });
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消删除",
              });
            });
        }
        //合同删除
        if (type === "合同") {
          this.$prompt("请输入删除原因", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(({ value }) => {
              //删除合同
              console.log(rowObj.contract_no);
              agreementdel(rowObj.contract_no, value)
                .then((res) => {
                  console.log("删除成功", res);
                })
                .catch((err) => {
                  console.log("请求失败", err);
                });
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消删除",
              });
            });
        }
        // 订单删除
        if (type === "订单") {
          console.log(rowObj.order_id);
          this.$prompt("请输入删除原因", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(({ value }) => {
              DelOrderAxios(value, rowObj.order_id)
                .then((res) => {
                  console.log("删除请求成功", res.data);
                  this.$message({
                    type: "success",
                    message: "删除成功",
                  });
                  // this.$router.go(0);
                  this.$emit("Delorder");
                })
                .catch((err) => {
                  console.log("删除请求失败：", err);
                  this.$message({
                    type: "error",
                    message: "删除失败",
                  });
                });
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消删除",
              });
            });
        }
        // 订单商品删除
        if (type === "订单商品") {
          console.log("rowObj", rowObj);
          this.$prompt("请输入删除原因", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(({ value }) => {
              DelOrderGoodsAxios(value, rowObj.order_goods_id, rowObj.orderid)
                .then((res) => {
                  console.log("删除请求成功", res.data);
                  this.$message({
                    type: "success",
                    message: "删除成功",
                  });
                  this.$emit("DelorderGoods");
                })
                .catch((err) => {
                  console.log("删除请求失败：", err);
                  this.$message({
                    type: "error",
                    message: "删除失败",
                  });
                });
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消删除",
              });
            });
        }
        // 财务删除
        if (type === "财务") {
          let id = rowObj.money_id;
          this.$prompt("请输入删除原因", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              getCustomerDataAxios(id)
                .then((res) => {
                  if (res.data.code == 200) {
                    this.$message({
                      type: "success",
                      message: "删除成功",
                    });
                    // 重新请求数据
                    this.$emit("refreshPage");
                  }
                })
                .catch((err) => {
                  console.log("请求失败：", err);
                  this.$message({
                    type: "error",
                    message: "删除失败",
                  });
                });
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消删除",
              });
            });
        }

        // 公海删除
        if (type === "公海") {
          console.log(rowObj.customer_id);
          this.$prompt("请输入删除原因", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(({ value }) => {
              poolListDeleteAxios({
                remark: value,
                customerIds: [rowObj.customer_id],
                delInfo: [0],
              })
                .then((res) => {
                  console.log("删除请求成功:");
                  console.log(res);
                  this.$message({
                    type: "success",
                    message: "删除成功",
                  });
                  this.$emit("Delpool");
                })
                .catch((err) => {
                  console.log("删除请求失败:", err);
                  this.$message({
                    type: "error",
                    message: "删除失败",
                  });
                });
            })
            .catch(() => {
              console.log("取消删除请求");
              this.$message({
                type: "info",
                message: "已取消删除",
              });
            });
        }
        //客户删除
        if (type === "客户") {
          this.$confirm("此操作将删除该文件, 是否继续?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }).then(() => {
            delCustomerAxios({
              customerIds: [rowObj.customer_id],
              delInfo: [1],
              remark: "客户删除",
            }).then(() => {
              this.$message({
                type: "success",
                message: "删除成功",
              });
              this.$emit("aa");
              console.log("aa");
            });
          });
        }
        //员工列表-删除
        if (type === "员工管理") {
          let id = rowObj.employee_login;
          this.$confirm("是否删除?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              delStaffListAxios(id).then((res) => {
                console.log("部门设置删除请求成功", res);
                console.log("rowObj.employee_login", rowObj.employee_login);
              });
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              window.location.reload();
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消删除",
              });
            });
        }
        //部门设置-删除
        if (type === "部门设置") {
          let name = rowObj.dept_name;
          this.$confirm("是否删除?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              delDepAxios(name).then((res) => {
                console.log("部门设置删除请求成功", res);
              });
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              window.location.reload();
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消删除",
              });
            });
        }
        //角色权限-删除
        if (type === "角色权限") {
          let name = rowObj.role_name;
          this.$confirm("是否删除?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              delRoleAxios(name).then((res) => {
              console.log("角色权限删除请求成功", res);
              
            });
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              window.location.reload();
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消删除",
              });
            });
        }
        // 售后删除
        if (type === "售后") {
          console.log(rowObj.a_s_id);
          this.$prompt("请输入删除原因", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(({ value }) => {
              console.log(value);
              afterSaleDeleteAxios(rowObj.a_s_id)
                .then((res) => {
                  console.log(res.data);
                  this.$message({
                    type: "success",
                    message: "删除成功",
                  });
                  this.$router.push("/afterSale/afterSaleProcessed");
                  this.$router.push("/afterSale/afterSaleList");
                })
                .catch((err) => {
                  console.log("请求失败：", err);
                  this.$message({
                    type: "error",
                    message: "删除失败",
                  });
                });
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消删除",
              });
            });
        }
      } else if (btnName === "续费") {
        // 合同续费
        if (type === "合同") {
          // console.log(rowObj.contract_id);
          this.$router.push({ path: "/agreement/renew", query: rowObj });
        }
      } else if (btnName === "到款") {
        //合同到款
        if (type === "合同") {
          console.log(rowObj);
          this.$alert(
            '<div class="div1"><span>到款金额</span> <input class="input1" id="input1"v-model="input1"><span>元</span></div> <div class="div2"><span>备注</span><textarea class="input2" id="input2">',
            "新增到款",
            {
              dangerouslyUseHTMLString: true,
            }
          ).then(() => {
            this.input1 = document.getElementById("input1").value;
            // console.log(this.input1);
            this.input2 = document.getElementById("input2").value;
            // console.log(this.input2);
            contranctReceivePayment(this.input1, rowObj.contract_id)
              .then((res) => {
                console.log("请求成功", res);
                this.$router.go(0);
              })
              .catch((err) => {
                console.log("请求失败", err);
              });

            document.getElementById("input1").value = 0;
            document.getElementById("input2").value = "";
          });
        }
        //订单到款
        if (type === "订单") {
          console.log(rowObj.order_id);
          console.log(rowObj);
          this.$alert(
            '<div class="div1"><span>到款金额</span> <input class="input1" id="input1"v-model="input1"><span>元</span></div> <div class="div2"><span>备注</span><textarea class="input2" id="input2">',
            "新增到款",
            {
              dangerouslyUseHTMLString: true,
            }
          ).then(() => {
            this.input1 = document.getElementById("input1").value;
            // console.log(this.input1);
            this.input2 = document.getElementById("input2").value;
            // console.log(this.input2);
            EditOrderMoneyAxios(this.input1, rowObj.order_id)
              .then((res) => {
                console.log("订单修改到款金额请求成功", res);
                this.$emit("OrderReceived");
              })
              .catch((err) => {
                console.log("订单修改到款金额请求失败", err);
              });

            document.getElementById("input1").value = 0;
            document.getElementById("input2").value = "";
          });
        }
      } else if (btnName === "打印") {
        // 订单打印
        if (type === "订单") {
          console.log(rowObj.order_id);
        }
      }

      //回收站管理
      else if (btnName === "权限") {
        if (type === "员工管理") {
          this.$router.push("/staff/permissions");
        }
      } else if (btnName === "管理范围") {
        if (type === "员工管理") {
          this.$router.push("/staff/management-range");
        }
      } else if (btnName === "还原") {
        console.log("还原");
        reductionDeletedAxios(rowObj.id)
          .then((res) => {
            if (res.data.code === 200) {
              console.log(res.data);
              window.location.reload();
            }
          })
          .catch((err) => {
            console.log("请求还原失败:", err);
          });
      } else if (btnName === "转入公海") {
        console.log("转入公海");
        tochageDeletedAxios(rowObj.id)
          .then((res) => {
            if (res.data.code === 200) {
              console.log(res.data);
              window.location.reload();
            }
          })
          .catch((err) => {
            console.log("请求转入公海失败:", err);
          });
      } else if (btnName === "彻底删除") {
        console.log("彻底删除");
        thoroughDeletedAxios(rowObj.id)
          .then((res) => {
            if (res.data.code === 200) {
              console.log(res.data);
              window.location.reload();
            }
          })
          .catch((err) => {
            console.log("请求失败：", err);
          });
      } else if (btnName === "修改审核状态") {
        if (type == "财务") {
          let id = rowObj.money_id;
          if (rowObj.check == "待审核") {
            this.$prompt("请输入审核请求：", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "success",
            })
              .then(() => {
                this.getStateData({
                  // 0：待审核，1：已审核
                  finance_No: id, // 代表删除哪一行的id
                  finance_State: 1, // 传递参数代表是什么操作
                })
                  .then(() => {
                    this.$message({
                      type: "success",
                      message: "审核成功",
                    });
                    // 重新请求数据
                    this.$emit("refreshPage");
                  })
                  .catch((err) => {
                    console.log("请求失败：", err);
                    this.$message({
                      type: "error",
                      message: "审核失败",
                    });
                  });
              })
              .catch(() => {
                this.$message({
                  type: "info",
                  message: "已取消审核",
                });
              });
          } else if (rowObj.check == "已审核") {
            this.$prompt("确定取消审核吗？", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            })
              .then(() => {
                setStateAxios({
                  // 0：待审核，1：已审核
                  finance_No: id, // 代表删除哪一行的id
                  finance_State: 0, // 传递参数代表是什么操作
                })
                  .then(() => {
                    this.$message({
                      type: "success",
                      message: "待审核中",
                    });
                    // 重新请求数据
                    this.$emit("refreshPage");
                  })
                  .catch((err) => {
                    console.log("请求失败：", err);
                    this.$message({
                      type: "error",
                      message: "取消审核失败",
                    });
                  });
              })
              .catch(() => {
                this.$message({
                  type: "info",
                  message: "已取消审核",
                });
              });
          }
        }
      }

      // 公海审核
      else if (btnName === "申请") {
        if (type === "公海") {
          console.log(rowObj.cp_id);
          this.$confirm("进行申请吗？", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "success",
          })
            .then(() => {
              ApplyingAxios([rowObj.cp_id.toString()])
                .then((res) => {
                  console.log(res.data);
                  this.$message({
                    type: "SUCCESS",
                    message: "请求申请成功",
                  });
                })
                .catch((err) => {
                  console.log("请求审核通过失败:", err);
                  this.$message({
                    type: "error",
                    message: "请求申请失败",
                  });
                });
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消审核通过",
              });
            });
        }
      } else if (btnName === "通过") {
        if (type === "公海") {
          console.log(rowObj.customer_name);
          this.$confirm("确定通过吗？", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "success",
          })
            .then(() => {
              AuditResultAxios([
                // 2拒绝 3通过
                {
                  cp_id: rowObj.cp_id,
                  application_status: 3,
                  customer: {
                    customer_id: rowObj.customer_id,
                  },
                  employee: {
                    employee_id: rowObj.employee_id,
                  },
                },
              ])
                .then((res) => {
                  console.log(res);
                  this.$message({
                    type: "SUCCESS",
                    message: "请求审核通过成功",
                  });
                })
                .catch((err) => {
                  console.log("请求审核通过失败:", err);
                  this.$message({
                    type: "error",
                    message: "请求审核通过失败",
                  });
                });
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消审核通过",
              });
            });
        }
      } else if (btnName === "拒绝") {
        if (type === "公海") {
          console.log(rowObj.customer_name);
          this.$confirm("确定拒绝吗？", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "success",
          })
            .then(() => {
              AuditResultAxios([
                {
                  // 2拒绝 3通过
                  customer_id: rowObj.customer_id,
                  application_status: 2,
                  employee_id: rowObj.employee_id,
                  cp_id: rowObj.cp_id,
                },
              ])
                .then(() => {
                  this.$message({
                    type: "SUCCESS",
                    message: "请求审核拒绝成功",
                  });
                })
                .catch((err) => {
                  console.log("请求审核拒绝失败:", err);
                  this.$message({
                    type: "error",
                    message: "请求审核拒绝失败",
                  });
                });
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消审核拒绝",
              });
            });
        }
      } else if (btnName === "详情") {
        if (type === "公海") {
          console.log(rowObj.customer_name);
          this.$router.push({
            path: "/customerseas/SeasDetails",
            query: { poollistMsg: rowObj },
          });
        }
      }
    },
  },
};
</script>

<style>
.div1,
.div2 {
  display: flex;
  flex-wrap: wrap;
  line-height: 80px;
  margin-top: 10px;
  padding: 0px 0px 0px 5px;
}
.input1 {
  width: 200px;
  height: 35px;
  margin-top: 18px;
  margin-left: 20px;
  margin-right: 10px;
  outline: none;
  border: 1px solid #000;
  border-radius: 5px;
  padding: 5px;
  box-sizing: border-box;
}
.input2 {
  width: 300px;
  height: 80px;
  margin-top: 10px;
  outline: none;
  margin-left: 28px;
  border-radius: 5px;
  padding: 10px;
  box-sizing: border-box;
}
</style>